<%@ page contentType="text/html" %>
<%@ page pageEncoding="UTF-8" %>

<link rel="stylesheet" type="text/css" href="js/yui/assets/skins/sam/paginator.css" />
<link rel="stylesheet" type="text/css" href="js/yui/assets/skins/sam/datatable.css" />

<script type="text/javascript" src="js/yahoo-dom-event.js"></script>
<script type="text/javascript" src="js/yui/connection/connection-min.js"></script>
<script type="text/javascript" src="js/yui/json/json-min.js"></script>
<script type="text/javascript" src="js/yui/element/element-min.js"></script>
<script type="text/javascript" src="js/yui/paginator/paginator-min.js"></script>
<script type="text/javascript" src="js/yui/datasource/datasource-min.js"></script>
<script type="text/javascript" src="js/yui/datatable/datatable-min.js"></script>
<script type="text/javascript">
	function selectOptionByKey(selectObj, keyValue) {
		var options = selectObj.options;
		for ( var i = 0; i < options.length; i++) {
			if (keyValue == options[i].value) {
				selectObj.selectedIndex = i;
				break;
			}
		}
		;
	}

	YAHOO.example.DynamicData = function() {
		// Column definitions
		var myColumnDefs = [ //sortable:true enables sorting         
				  { key : "linkId", label : "<input type='checkbox' name='verifyAll' id='verifyAll' onclick='boVerifyAll(this);'/>",
					width : "30", parser : "string" }
				, { key : "linkName", label : "Menu Name",
					resizeable : true, width : 120, parser : "string" }
				, { key : "linkUrl", label : "Menu Path",
					resizeable : true, width : 120, parser : "string" }
				, { key : "linkLevel", label : "Menu Level",
					resizeable : true, width : 220, parser : "string" }
				, { key : "displayOrder", label : "Order",
					resizeable : true, width : 120, parser : "string" }
				, { key : "linkParentId", label : "Parent Menu",
					resizeable : true, width : 110, parser : "string" }
		];
		
		// Custom parser
		var stringToDate = function(sData) {
			var array = sData.split("-");
			return new Date(array[1] + " " + array[0] + ", " + array[2]);
		};

		// DataSource instance
		//var myDataSource = new YAHOO.util.DataSource("http://localhost:8083/Repo/MaintainCounterPartyType_getTable.action?");
		var myDataSource = new YAHOO.util.DataSource(
				"manageMenu_getTable.action?");
		myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
		myDataSource.responseSchema = {
			resultsList : "object.records.detail",
			fields : [ {
				key : "linkId"
			}, {
				key : "linkName"
			}, {
				key : "linkUrl"
			}, {
				key : "linkLevel"
			}, {
				key : "displayOrder"
			}, {
				key : "linkParentId"
			} ],
			metaFields : {
				recordsReturned : "object.recordsReturned",
				totalRecords : "object.totalRecords", // Access to value in the server response
				startIndex : "object.startIndex:",
				//sort: "object.sort",
				dir : "object.dir",
				pageSize : "object.pageSize"
			}
		};

		// DataTable configuration
		var myConfigs = {
			initialRequest : "sort=id&dir=asc&startIndex=0&results=10", // Initial request for first page of data
			dynamicData : true, // Enables dynamic server-driven data
			//sortedBy : {key:"id", dir:YAHOO.widget.DataTable.CLASS_ASC}, // Sets UI initial sort arrow
			paginator : new YAHOO.widget.Paginator( {
				rowsPerPage : 10,
				containers : 'manageMenuTablePaging'
			})
		};

		// DataTable instance
		var myDataTable = new YAHOO.widget.DataTable("manageMenuTableDIV",
				myColumnDefs, myDataSource, myConfigs);

		myDataTable.set("selectionMode", "single");
		myDataTable.subscribe("rowClickEvent", myDataTable.onEventSelectRow);
		var outputSelections = function(oArgs) {

			//window.alert(this.getSelectedRows());
			var sr = this.getSelectedRows();
			var rec = this.getRecord(sr[0]);

			document.frmDealer["dealer.id"].value = rec.getData("id");
			document.frmDealer["dealer.dealerCode"].value = rec
					.getData("dealerCode");
			document.frmDealer["dealer.dealerName"].value = rec
					.getData("dealerName");
			document.frmDealer["dealer.createBy"].value = rec
					.getData("createBy");

			//descp = rec.getData("description");
			//window.alert(descp);

			//window.alert(document.f1.aaa.value);
			var thisselection = oArgs.els;

			thisselection = thisselection.join(", ");

			var allrecords = this.getSelectedRecordIds();
			allrecords = allrecords.join(", ");

			var allrows = this.getSelectedRows();
			allrows = allrows.join(", ");
		}
		myDataTable.subscribe("rowClickEvent", outputSelections);

		YAHOO.util.Event.on('addrow', 'click', function() {
			myDataTable.addRow( {
				foo : i,
				bar : 'bar:' + i,
				baz : 'new row'
			}, 0);
			++i;
		});
		//subscribe using the Paginator as execution scope
		//myDataTable.subscribe('rowAddEvent',function () {
		//this.setPage(this.getTotalPages());
		//this.setPage(0);
		//},myPaginator,true);

		// Update totalRecords on the fly with value from server
		myDataTable.handleDataReturnPayload = function(oRequest, oResponse,
				oPayload) {
			oPayload.totalRecords = parseInt(oResponse.meta.totalRecords);
			//oPayload.totalRecords = "40";
			//window.alert(oPayload.totalRecords);

			return oPayload;
		}

		return {
			ds : myDataSource,
			dt : myDataTable
		};

	}();
</script>
